<?php
namespace Food\Controller;

class AdminController extends \Common\Controller\BaseController{

	public function index(){
		$Food=D('FoodView');
		$count  = $Food->count();// 查询满足要求的总记录数
		$Page   = new \Com\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
		$show   = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$foodlist = $Food->limit($Page->firstRow.','.$Page->listRows)->order('fid desc')->select();
		$this->assign('page',$show);// 赋值分页输出

		$this->assign('foodlist',$foodlist);
		$this->display();
	}

	public function add(){
		if (IS_POST) {
			$Food=D("Food");
			$map['fname']=$_POST['fname'];//菜名
			$map['fcid']=$_POST['fcid'];
			$map['ftitle']=$_POST['ftitle'];//特点
			$map['fcontent']=$_POST['fcontent'];//菜品内容
			$map['fsort']=$_POST['fsort'];
			$map['fprice']=$_POST['fprice'];
			if ($_FILES["pic"]["name"]){ //图片为空不做图片处理	
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize = 3145728 ;// 设置附件上传大小
				$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->rootPath = './Upload/fimg/'; // 设置附件上传根目录
				$upload->savePath = ''; // 设置附件上传（子）目录
				// 上传文件
				$info = $upload->upload();
				if(!$info) {// 上传错误提示错误信息
					$this->error($upload->getError());
				}

				$map['fpic']='./Upload/fimg/'.$info['pic']['savepath'].$info['pic']['savename'];
				$image = new \Think\Image();
				$image->open($map['fpic']);
				$image->thumb(390, 330,\Think\Image::IMAGE_THUMB_CENTER)->save($map['fpic'].".thumb.jpg");
			}
			$map['fctime']=time();

			if (!$Food->create($map)){
				// 如果创建失败 表示验证没有通过 输出错误提示信息
				$this->error($Food->getError());
			}else{
				// 验证通过 可以进行其他数据操作
				$result=$Food->add($map);
				$this->success('操作成功');
			}	 
		}else{
			$Foodcat=M('Foodcat');
			$foodcatlist=$Foodcat->select();

			$this->assign('foodcatlist',$foodcatlist);
			$this->display();
		}
	}

	public function edit(){
		if (IS_POST) {
			$id = I('fid');
			if(!$id){
				//判定ＩＤ是否为空
				$this->error('菜品id不可以为空');
			}
			$data["fid"] = $id;
			$maps['fid'] = array('neq',$id);

			$Food=D("Food");

			$map['fname']    = $_POST['fname'];//菜名
			$map['fcid']     = $_POST['fcid'];
			$map['ftitle']   = $_POST['ftitle'];//特点
			$map['fcontent'] = $_POST['fcontent'];//菜品内容
			$map['fsort']    = $_POST['fsort'];
			$map['fprice']   = $_POST['fprice'];
			if ($_FILES["pic"]["name"]){ //图片为空不做图片处理	
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize = 3145728 ;// 设置附件上传大小
				$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->rootPath = './Upload/fimg/'; // 设置附件上传根目录
				$upload->savePath = ''; // 设置附件上传（子）目录
				// 上传文件
				$info = $upload->upload();
				if(!$info) {// 上传错误提示错误信息
					$this->error($upload->getError());
				}

				$map['fpic']='./Upload/fimg/'.$info['pic']['savepath'].$info['pic']['savename'];
				$image = new \Think\Image();
				$image->open($map['fpic']);
				$image->thumb(390, 330,\Think\Image::IMAGE_THUMB_CENTER)->save($map['fpic'].".thumb.jpg");
			}
			$map['fctime'] = time();


			if (!$Food->where($maps)->create($map)){
				// 如果创建失败 表示验证没有通过 输出错误提示信息
				$this->error($Food->getError());
			}else{
				// 验证通过 可以进行其他数据操作
				$result=$Food->where($data)->save($map);  
				$this->success('操作成功',U('Food/Admin/index'));
			}
		}else{
			$Food=M('Food');
			$map['fid']=$_GET['id'];	    
			$fooditem=$Food->where($map)->find();
			$Foodcat=M('Foodcat');
			$foodcatlist=$Foodcat->select();

			$this->assign('foodcatlist',$foodcatlist);

			$this->assign('item',$fooditem);
			$this->display('add');
		}
	}

	public function del(){
		$fid = I('id');
		$Food=D("Food");

		$result = $Food->where(array(
			'fid'  => $fid,
		))->delete();
		if ($result) {
			$this->success("删除成功！");
		}else{
			$this->error("删除失败！");
		}
	}

	public function recommend(){
		$fid = I('id');
		$status = I('recom');
		$Food=D("Food");

		$result = $Food->where(array(
			'fid'  => $fid,
		))->save(array('isrecommend'=>$status));
		if ($result) {
			$this->success("操作成功！");
		}else{
			$this->error("操作失败！");
		}
	}

	public function cate(){
		$Foodcat=M('Foodcat');
		$foodcatlist=$Foodcat->select();
		$this->assign('foodcatlist',$foodcatlist);

		$this->display();
	}

	public function add_cate(){
		if (IS_POST) {
			$Foodcat=M('Foodcat');
			$map['fcname']=$_POST['fcname'];
			$map['fcsort']=$_POST['fcsort'];
			$map['ctime']=time();

			if($map['fcname']){
				$result=$Foodcat->add($map);
				if ($result){
					$this->success('添加成功！',U('Food/Admin/cate'));
				}else{
					//错误后返回错误的操作状态和提示信息
					$this->error('新增失败');
				}
			}else {
				$this->error('分类名不可以为空');
			}
		}else{
			$this->display();
		}
	}

	public function edit_cate(){
		if (IS_POST) {
			$Foodcat=M('Foodcat');
			$map['fcid']=$_POST['fcid'];
			$data['fcname']=$_POST['fcname'];
			$data['fcsort']=$_POST['fcsort'];	    
			$result = $Foodcat->where($map)->save($data);

			if ($result){
				$this->success('修改成功！',U('Food/Admin/cate'));
			}else{
				//错误后返回错误的操作状态和提示信息
				$this->error('修改失败');
			}
		}else{
			$Foodcat=M('Foodcat');
			$map['fcid']=$_GET['id'];
			$foodcatitem=$Foodcat->where($map)->find();
			$this->assign('item',$foodcatitem);
			$this->display('Admin_add_cate');
		}
	}

	public function del_cate(){
		$Foodcat=M('Foodcat');
		$map['fcid']=$_GET['id'];
		$result = $Foodcat->where($map)->delete(); 

		if ($result) {
			$this->success("删除成功！");
		}else{
			$this->error("删除失败！");
		}
	}

	public function order(){
		$Orders=D('Foodorder');// 实例化User对象
		$orderlist=$Orders->select();

		$count      = $Orders->count();// 查询满足要求的总记录数
		$Page       = new \Com\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数

		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$orderlist = $Orders->order('oid desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('orderlist',$orderlist);// 赋值数据集;
		$this->assign('page',$show);// 赋值分页输出
		$this->display(); // 输出模板
	}

	public function orderdetail(){
		$id=I('id');
		$data['oid']=$id;
		$Orders=D('Foodorder');// 实例化User对象
		$orderdetail = $Orders->relation(true)->where($data)->find();
		$this->assign('orderdetail',$orderdetail);// 赋值数据集
		$this->display(); // 输出模板
	}

	//出单操作将订单状态变成完成
	public function orderend(){
		$id=I('id');
		$Orders=M('Foodorder');
		$map['oid']=$id;
		$data['orderstatus']=4;
		$data['order_endtime']=time();
		$result = $Orders->where($map)->save($data);
		if($result){ 
			$this->success("操作成功！",U('Food/Admin/order'));
		} else {
			$this->error('操作失败');
		}
	} 
}